package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.permission;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.RolesLevelEnum;
import com.kingyun.gpsinspection.purificationservice.common.enums.TreeIconSkinEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.RolesMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.organization.JsmOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmAccountRoleService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmRolesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian on 2017/1/5/005.
 * 角色服务实现层，控制角色的接口 和 增删改查
 */

public class JsmRolesServiceImpl implements JsmRolesService {

    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private JsmAccountRoleService accountRoleService;

    /**
     * @param account
     * @param systemId
     * @return 根据当前账户获取角色
     * @throws Exception
     */
    @Override
    public List<Roles> getPermissionRoles(Account account, String systemId) throws Exception {
        return null;
    }


    /**
     * 获取所有的角色
     */
    @Override
    public List<Roles> getAll() throws Exception {

        return rolesMapper.selectAll();
    }

    /**
     * @param roles 角色 新增
     * @return 角色新增或者角色修改
     * @throws Exception
     */
    @Override
    public Integer rolesAdd(Roles roles) throws Exception {
        Integer resInt = 0;
        if (roles.getRoleId() == null) {
            roles.setRoleId(UniqueUtil.uuid());
            resInt = rolesMapper.insertSelective(roles);
        } else {
            resInt = rolesMapper.updateByPrimaryKeySelective(roles);
        }
        return resInt;
    }

    /**
     * @param roleId 角色iD
     * @return
     * @throws Exception
     */
    @Override
    public Integer rolesDel(String roleId) throws Exception {
        return rolesMapper.deleteByPrimaryKey(roleId);
    }

    /**
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public Roles rolesSelect(String roleId) throws Exception {
        return rolesMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public List<Roles> rolesListSelect(String systemCode, int roleType) {
        Map map = new HashMap<>();
        map.put("systemCode", systemCode);
        map.put("roleType", roleType);
        return rolesMapper.rolesListSelect(map);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量插入
     * @Date 2017/2/8 17:58
     */
    @Override
    public int insertList(List<Roles> list) {
        return rolesMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<Roles> list) {
        return rolesMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:58
     */
    @Override
    public int deleteList(List<Roles> list) {
        return rolesMapper.deleteList(list);
    }

    /**
     * @param keyword 搜索关键值
     * @param roleId  角色Id {父级角色}
     * @return
     * @throws Exception
     */
    @Override
    public List<Roles> getChildRenRoles(String keyword, String roleId) throws Exception {
        Roles roles = new Roles();
        roles.setParentRoleId(roleId);
        return rolesMapper.getChildRenRoles(roles);
    }

    /**
     * @param keyword 搜索关键值
     * @param roleId  角色Id {父级角色}
     */
    @Override
    public List<Ztree> getZtreeChildRenRoles(String keyword, String roleId) throws Exception {


        return offsetZtree(getChildRenRoles(keyword, roleId));
    }


    /**
     * @param orgId      组织机构层级显示
     * @param isLoadOrg  是否加载组织机构
     * @param isLoadRole 是否加载角色
     * @return 返回角色树
     */
    @Override
    public List<Ztree> ajaxLoadRoles(String orgId, boolean isLoadOrg, boolean isLoadRole, Employee employee) throws Exception {
        List<Ztree> ztreest = new ArrayList<>();
        // 组织机构,  根据组织机构加载出所有的角色
        if (orgId != null) {
            Map selMap = new HashMap<>();
            selMap.put("orgId", orgId);
            // 判断当前人是否是超级管理员
            boolean isAdmin = isAdmin(employee);
            selMap.put("isAdmin", isAdmin);
            List<Roles> rolesList = rolesMapper.rolesListSelect(selMap);
            List<Ztree> ztreeList = offsetZtree(rolesList);
            ztreest.addAll(ztreeList);
            List<Organization> organizationList = organizationService.queryNodeByOrgId(orgId);
            ztreest.addAll(organizationService.setZtree(organizationList));
        } else {
            // 组织机构为空
            Organization organizationServices = organizationService.findOrgById(employee.getOrgId());
            List<Organization> organizations = new ArrayList<>();
            organizations.add(organizationServices);
            ztreest.addAll(organizationService.setZtree(organizations));
        }
        return ztreest;
    }

    /**
     * @param employee 当前人员信息
     * @param keyword 高级搜索问题
     * 角色搜索问题,进行角色名称进行查询
     */
    @Override
    public List<Ztree> noAjaxLoadRoles(Employee employee, String keyword) throws Exception {
        List<Ztree> rolesArrayList = new ArrayList<>();
        List<String> roleOrgList = new ArrayList<>();
        /**
         * 根据当前搜索条件查询角色， 然后查询角色的上级组织，到达当前人员组织机构的等级即可
         */
        if(StringUtils.isNotEmpty(employee.getOrgId()) && StringUtils.isNotEmpty(keyword)){
            Roles rs = new Roles();
            rs.setOrgId(employee.getOrgId());
            rs.setRoleName(keyword);
            List<Roles> rolesList = rolesMapper.selectByKeyWord(rs);
            // 查询当前角色 ， 查询出来
            if(rolesList!=null && rolesList.size() > 0){
                // 进行角色查询
                for(Roles rsRoles : rolesList){
                    Ztree ztree = new Ztree();
                    ztree.setId(rsRoles.getRoleId());
                    ztree.setName(rsRoles.getRoleName());
                    ztree.setpId((rsRoles.getParentRoleId()!=null)?rsRoles.getParentRoleId():rsRoles.getOrgId());
                    ztree.setOpen(true);
                    ztree.setIsParent(false);
                    ztree.setIconSkin(TreeIconSkinEnum.ICON_ROLES.getDesc());
                    rolesArrayList.add(ztree);
                    roleOrgList.add(rsRoles.getOrgId());
                }
            }

        }

        // 查询出来组织机构不为空这显示出当前角色的组织机构为最大组织机构
        if(roleOrgList != null && roleOrgList.size() > 0){
            // 查询当前组织机构决定性问题
            List<Organization> organizationList =  organizationService.selectBottomToTopOrgList(roleOrgList,employee);
            List<Ztree> orgZtree = organizationService.setZtree(organizationList);
            rolesArrayList.addAll(orgZtree);
        }
        return rolesArrayList;
    }

    /**
     * 根据人员
     * @param employee 人员信息判断，是否为超级管理员
     */
    @Override
    public boolean isAdmin(Employee employee) throws Exception {
        List<AccountRole> accountRoleList = accountRoleService.getRolesByAccount(employee);
        boolean flag = false;
        // 判断当前人员角色信息问题;
        if (accountRoleList != null) {
            for (AccountRole ar : accountRoleList) {
                Roles roles = rolesMapper.selectByPrimaryKey(ar.getRoleId());
                // 判断当前角色是否存在 ， 当前角色
                if (roles != null) {
                    // 判断当前角色 是否为超级管理员 ； 或者系统管理员
                    if (roles.getRoleType().intValue() == RolesLevelEnum.ROLES_S.getCode()) flag = true;
                }
            }
        }
        return flag;
    }

    /**
     * 根据条件查询ID集合
     *
     * @param list
     * @return
     */
    @Override
    public List<String> selectByListId(List<Roles> list) {
        return rolesMapper.selectByListId(list);
    }

    /**
     * 将角色信息转换成角色树
     */
    public List<Ztree> offsetZtree(List<Roles> rolesList) {
        List<Ztree> list = new ArrayList<>();
        if (rolesList != null) {
            for (Roles roles : rolesList) {
                Ztree ztree = new Ztree();
                ztree.setId(roles.getRoleId());
                ztree.setpId(roles.getParentRoleId());
                ztree.setName(roles.getRoleName());
                ztree.setIconSkin(TreeIconSkinEnum.ICON_ROLES.getDesc());
                ztree.setIsParent(false);
                ztree.setOpen(true);
                list.add(ztree);
            }
        }
        return list;
    }


}
